import NetAdapter from "../adpapter/NetAdapter";

export default class DeviceManager {

    private static _instance: DeviceManager = null;
    public static get inst(): DeviceManager {
        return this._instance || (this._instance = new DeviceManager());
    }

    public isAndroid = cc.sys.isNative && cc.sys.os === cc.sys.OS_ANDROID;
    public isIOS = cc.sys.isNative && cc.sys.os === cc.sys.OS_IOS;
    public get isDouyinMiniGame() {
        if (!window["tt"] || window["tt"] == "undefined" || this.isAndroid || this.isIOS) {
            return false;
        }
        return true;
    }
    public get isKuaishouMiniGame() {
        if (!window["ks"] || window["ks"] == "undefined" || this.isAndroid || this.isIOS) {
            return false;
        }
        return true;
    }
    public get isMoNiQi() {
        let b = false;
        if (this.isAndroid) {
            b = jsb.reflection.callStaticMethod("com/jingyingxiaobing/blueclient/AppActivity", "isMoNiQi", "()Ljava/lang/String;") == "1";
        }
        return b;
    }
    public get deviceType() {
        let type = "";
        if (this.isAndroid) {
            type = "android";
        } else if (this.isIOS) {
            type = "ios";
        }
        if (this.isMoNiQi) {
            type += "Simulator"
        }
        return type;
    }

    public copyText(text: string, ok?: Function, fail?: Function) {
        if (this.isAndroid) {
            jsb.reflection.callStaticMethod("com/jingyingxiaobing/blueclient/AppActivity", "copyToClipboard", "(Ljava/lang/String;)V", text);
            ok && ok();
        } else if (this.isIOS) {
            jsb.reflection.callStaticMethod("AppController", "copyToClipboard:", text);
            ok && ok();
        } else if (window["tt"]) {
            //@ts-ignore
            tt.setClipboardData({
                data: text,
                success(res) {
                    console.log(`setClipboardData调用成功`);
                    ok && ok();
                },
                fail(res) {
                    console.log(`setClipboardData调用失败`);
                    fail && fail();
                },
            });
        } else if (window["ks"]) {
            //@ts-ignore
            ks.setClipboardData({
                data: text,
                success(res) {
                    console.log(`setClipboardData调用成功`);
                    ok && ok();
                },
                fail(res) {
                    console.log(`setClipboardData调用失败`);
                    fail && fail();
                },
            });
        }
    }

    public getContentFromClipboard() {
        let str = "";
        if (this.isAndroid) {
            str = jsb.reflection.callStaticMethod("com/jingyingxiaobing/blueclient/AppActivity", "getInfoFormClipBoard", "()Ljava/lang/String;");
        } else if (this.isIOS) {
            str = jsb.reflection.callStaticMethod("AppController", "getContentFromClipboard");
        }
        return str || "";
    }

    getIdCallback: Function;
    getId(callback: Function) {
        this.getIdCallback = callback;
        if (this.isAndroid) {
            jsb.reflection.callStaticMethod("com/jingyingxiaobing/blueclient/AppActivity", "getDeviceId", "()V");
        } else if (this.isIOS) {
            let vendorID = jsb.reflection.callStaticMethod("AppController", "getVendorID");
            this.getIdResult(vendorID);
        } else {
            // this.getIdResult(Date.now().toString());
            this.getIdResult("test123456789");
        }
    }
    getIdResult(id: string) {
        console.log("getIdResult", id);
        if (this.getIdCallback) {
            this.getIdCallback(id);
            this.getIdCallback = null;
        }
    }

    hasSimCard() {
        let result = true;
        if (this.isAndroid) {
            result = jsb.reflection.callStaticMethod("com/jingyingxiaobing/blueclient/AppActivity", "hasSimCard", "()I") == 1;
        } else if (this.isIOS) {
            // result = jsb.reflection.callStaticMethod("AppController", "hasSimCard") == 1;
        } else {

        }
        return result;
    }

    openBower(url) {
        if (this.isAndroid) {
            jsb.reflection.callStaticMethod("com/jingyingxiaobing/blueclient/AppActivity", "openBower", "(Ljava/lang/String;)V", url);
        } else if (this.isIOS) {
            jsb.reflection.callStaticMethod("AppController", "openDefaultBrowserWithURL:", url);
        } else if (cc.sys.isBrowser) {
            //@ts-ignore
            window.open(url);
        }
    }

    zfbOpenAuthSchemeCallback: Function;
    zfbOpenAuthSchemeErrorCallback: Function;
    zfbOpenAuthScheme(id_card_no: string, name: string, callback: Function, error?: Function) {
        this.zfbOpenAuthSchemeCallback = callback;
        this.zfbOpenAuthSchemeErrorCallback = error;
        let func = (cert_verify_id) => {
            if (this.isAndroid) {
                jsb.reflection.callStaticMethod("com/jingyingxiaobing/blueclient/AppActivity", "zfbOpenAuthScheme", "(Ljava/lang/String;)V", cert_verify_id);
            } else if (this.isIOS) {
                jsb.reflection.callStaticMethod("AppController", "zfbOpenAuthScheme:", cert_verify_id);
            }
        }
        func("123456");
        // let send = { id_card_no, name }
        // console.log("支付宝录入用户实名信息", JSON.stringify(send));
        // NetAdapter.authenticationUser(send).then((res: any) => {
        //     console.log("authenticationUser res:", JSON.stringify(res));
        //     let cert_verify_id = res.info.verify_id;
        //     if (!cert_verify_id) {
        //         console.log("缺少cert_verify_id");
        //         return;
        //     }
        //     console.log("cert_verify_id:", cert_verify_id);
        //     func(cert_verify_id);
        // })
    }
    zfbOpenAuthSchemeResult(code) {
        console.log("支付宝授权code", code);
        // NetAdapter.authenticationCode({ code }).then((res: any) => {
        //     console.log("支付宝授权返回信息authenticationCode", JSON.stringify(res));
        //     if (this.zfbOpenAuthSchemeCallback) {
        //         this.zfbOpenAuthSchemeCallback(code);
        //         this.zfbOpenAuthSchemeCallback = null;
        //     }
        // }).catch(err => {
        //     if (this.zfbOpenAuthSchemeErrorCallback) {
        //         this.zfbOpenAuthSchemeErrorCallback(code);
        //         this.zfbOpenAuthSchemeErrorCallback = null;
        //     }
        // })

        if (this.zfbOpenAuthSchemeCallback) {
            this.zfbOpenAuthSchemeCallback(code);
            this.zfbOpenAuthSchemeCallback = null;
        }
        NetAdapter.zfbAuthentication().then((res: any) => {
        })
    }
}

cc["DeviceManager"] = DeviceManager;
window["DeviceManager"] = DeviceManager;